package com.yipei.business.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipei.business.domain.MlTradeDealOrder;
import com.yipei.business.mapper.MlTradeDealOrderMapper;
import com.yipei.business.vo.MlKLineMasterVo;
import com.yipei.common.utils.DateUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yipei.business.mapper.MlSymbolsInfoMapper;
import com.yipei.business.domain.MlSymbolsInfo;
import com.yipei.business.service.IMlSymbolsInfoService;

/**
 * 标的信息Service业务层处理
 *
 * @author yipei
 * @date 2024-12-23
 */
@Service
public class MlSymbolsInfoServiceImpl extends ServiceImpl<MlSymbolsInfoMapper, MlSymbolsInfo> implements IMlSymbolsInfoService
{
    @Autowired
    private MlSymbolsInfoMapper mlSymbolsInfoMapper;

    /**
     * 查询标的信息
     *
     * @param id 标的信息主键
     * @return 标的信息
     */
    @Override
    public MlSymbolsInfo selectMlSymbolsInfoById(Long id)
    {
        return mlSymbolsInfoMapper.selectMlSymbolsInfoById(id);
    }

    /**
     * 查询标的信息列表
     *
     * @param mlSymbolsInfo 标的信息
     * @return 标的信息
     */
    @Override
    public List<MlSymbolsInfo> selectMlSymbolsInfoList(MlSymbolsInfo mlSymbolsInfo)
    {
        return mlSymbolsInfoMapper.selectMlSymbolsInfoList(mlSymbolsInfo);
    }

    /**
     * 新增标的信息
     *
     * @param mlSymbolsInfo 标的信息
     * @return 结果
     */
    @Override
    public int insertMlSymbolsInfo(MlSymbolsInfo mlSymbolsInfo)
    {
        mlSymbolsInfo.setCreateTime(DateUtils.getNowDate());
        return mlSymbolsInfoMapper.insertMlSymbolsInfo(mlSymbolsInfo);
    }

    /**
     * 修改标的信息
     *
     * @param mlSymbolsInfo 标的信息
     * @return 结果
     */
    @Override
    public int updateMlSymbolsInfo(MlSymbolsInfo mlSymbolsInfo)
    {
        mlSymbolsInfo.setUpdateTime(DateUtils.getNowDate());
        return mlSymbolsInfoMapper.updateMlSymbolsInfo(mlSymbolsInfo);
    }

    /**
     * 批量删除标的信息
     *
     * @param ids 需要删除的标的信息主键
     * @return 结果
     */
    @Override
    public int deleteMlSymbolsInfoByIds(Long[] ids)
    {
        return mlSymbolsInfoMapper.deleteMlSymbolsInfoByIds(ids);
    }

    /**
     * 删除标的信息信息
     *
     * @param id 标的信息主键
     * @return 结果
     */
    @Override
    public int deleteMlSymbolsInfoById(Long id)
    {
        return mlSymbolsInfoMapper.deleteMlSymbolsInfoById(id);
    }

    @Override
    public void SymbolsInfo() throws IOException {
        int page = 1;
        boolean hasMore = true;
        List<MlSymbolsInfo> mlSymbolsInfos2= new ArrayList<>();
        while (hasMore) {
            HttpClient client = HttpClients.createDefault();
            HttpGet request = new HttpGet("https://87.push2.eastmoney.com/api/qt/clist/get?cb=jQuery&pn=" + page + "&pz=10000&po=1&np=1&fltt=2&invt=2&dect=1web&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048"); //示例URL，请替换为实际的股票代码
            request.setHeader("Content-Type", "text/javascript;charset=UTF-8");
            HttpResponse response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                List<MlSymbolsInfo> mlSymbolsInfos;
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity, "UTF-8").replace("jQuery", "");
                result = Stream.of(result.split(";")).collect(Collectors.joining(";"));
                result = result.substring(1, result.length() - 1);
                JSONObject jsonObject = JSONObject.parseObject(result).getJSONObject("data");
                JSONArray diff = jsonObject.getJSONArray("diff");
                mlSymbolsInfos = diff.toJavaList(MlSymbolsInfo.class);
                if (mlSymbolsInfos.size() < 100) {
                    hasMore = false; // 最后一页
                } else {
                    page++;
                }
                for (MlSymbolsInfo mlSymbolsInfo : mlSymbolsInfos) {
                    mlSymbolsInfo.setCreateTime(DateUtils.getNowDate());
                    mlSymbolsInfo.setSymbol(mlSymbolsInfo.getF12());
                    mlSymbolsInfo.setSecName(mlSymbolsInfo.getF14());
                    mlSymbolsInfo.setSecId(mlSymbolsInfo.getF12());
                }
                mlSymbolsInfos2.addAll(mlSymbolsInfos);
            }

        }
        List<MlSymbolsInfo> after= mlSymbolsInfos2.stream().filter(distinctByKey(MlSymbolsInfo::getSymbol)).collect(Collectors.toList());
        this.saveOrUpdateBatch(after,1000);
    }

        public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
            Map<Object, Boolean> seen = new ConcurrentHashMap<>();
            return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
        }


}
